<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;

class Deliver extends Model
{
    //
    protected $table = 'deliver';
    protected $guarded = [];

    public function position()
    {
        return $this->belongsTo('App\Models\Home\Position','position_id','id');
    }

    public function resume()
    {
        return $this->belongsTo('App\Models\Home\Resume','resume_id','id');
    }

    public function check_schedule(){

        return $this->hasOne('App\Models\Home\DeliverSchedule', 'deliver_id', 'id');
    }

    public function one_schedule(){

        return $this->hasOne('App\Models\Home\DeliverSchedule', 'deliver_id', 'id');
    }

    public function all_schedule(){

        return $this->hasMany('App\Models\Home\DeliverSchedule', 'deliver_id', 'id');
    }

    public function getList($skip,$limit,$opt=array(),$orm=null,$resume_opt=array()){

        if (!isset($limit)||!isset($skip)){
            return [];
        }
        if (!isset($skip)){
            $skip = 0;
        }

        $query = self::orderBy('id', 'desc');

        if (!empty($opt)) {
            $query->orWhere(function ($query) use ($opt) {
                foreach ($opt as $k => $v) {
                    if (is_array($v) && $v[1]) {
                        $query = $query->where($k, $v[0], $v[1]);
                    } elseif (!is_array($v) && $v !== false) {
                        $query = $query->where($k, $v);
                    }
                }
            });
        }

        $query = $query->with(['one_schedule'=>function($query){
            $query->orderby('id','desc');
        }]);

        if ($orm && $orm=='position'){

            $query = $query->with(['position'=>function($query){

                $query->with(['user','assign_user','client','position_class']);

            }]);

        }else if($orm && $orm=='resume'){

            $query = $query->with(['resume'=>function($query) use($resume_opt){

                if (!empty($resume_opt)){
                    foreach ($resume_opt as $k => $v){
                        if(is_array($v) && $v[1]){
                            $query = $query->where($k,$v[0],$v[1]);
                        }elseif(!is_array($v) && $v !== false){
                            $query = $query->where($k,$v);
                        }
                    }
                }

                $query->with(['user','assign_user','position_class']);

            }]);

        }

        $list = $query->skip($skip)->limit($limit)->get();


        foreach ($list as $k => $v){
            $item = [];
            if ($orm && $orm=='position'){
                $item = $v->position;

            }else if($orm && $orm=='resume'){
                $item = $v->resume;
            }

            if (!$item){
                unset($list[$k]);
                continue;
            }

            if ($orm && $orm=='position'){
                $list[$k]->client_name = $item->client?$item->client->chinese_name:'';

                switch ($v->position->emergency){
                    case 1:
                        $list[$k]->position->emergency_name = '紧急重要，优质客户';
                        break;
                    case 2:
                        $list[$k]->position->emergency_name = '紧急不重要，一般客户';
                        break;
                    case 3:
                        $list[$k]->position->emergency_name = '不紧急不重要，新客户';
                        break;
                    default:
                        $list[$k]->position->emergency_name = '';
                }

//                $list[$k]->position->category_name = $v->position->position_class?$v->position->position_class->name:'';
                $list[$k]->position->region_name = $v->position->region?config('base.country.'.$v->position->region.'.name'):'';
                $list[$k]->position->range_name = $v->position->range?config('base.working_salary.'.$v->position->range.'.name'):'';
                switch ($v->position->state){
                    case 0:
                        $list[$k]->position->state_name = 'closed';
                        break;
                    case 1:
                        $list[$k]->position->state_name = '招聘中';
                        break;
                    default:
                        $list[$k]->position->state_name = 'closed';
                }
            }else if($orm && $orm=='resume'){
                $user_type = $item->user?$item->user->type:0;
                switch ($user_type){
                    case 1:
                        $list[$k]->user_type = '内部推荐 - ';
                        break;
                    case 2:
                        $list[$k]->user_type = '兼职推荐 - ';
                        break;
                    default:
                        $list[$k]->user_type = '自投';
                }

                $list[$k]->resume->nationality_name = $v->resume->nationality?config('base.country.'.$v->resume->nationality.'.name'):'';
                $list[$k]->resume->education_name = $v->resume->education?config('base.education.'.$v->resume->education.'.name'):'';
                $list[$k]->resume->experience_name = $v->resume->experience?config('base.working_time.'.$v->resume->experience.'.name'):'';
                $list[$k]->resume->position_name = $v->resume->position_class?$v->resume->position_class->name:'';
                $list[$k]->resume->state_name = $v->resume->status?config('base.working_state.'.$v->resume->status.'.name'):'';

            }

            $list[$k]->username = $item->user?$item->user->username:'';
            $list[$k]->assign_username = $item->assign_user?$item->assign_user->username:'';
            $list[$k]->team_username = $item->assign_user?$item->assign_user->frame?$item->assign_user->frame->belong_user?$item->assign_user->frame->belong_user->username:'':'':'';

            $result = $v->one_schedule?$v->one_schedule->result:'';
            switch ($result){
                case 1:
                    $list[$k]->result_name = '待推荐';
                    break;
                case 2:
                    $list[$k]->result_name = '推荐客户';
                    break;
                case 3:
                    $list[$k]->result_name = '筛选通过';
                    break;
                case 4:
                    $list[$k]->result_name = '安排一面';
                    break;
                case 5:
                    $list[$k]->result_name = '一面fail';
                    break;
                case 6:
                    $list[$k]->result_name = '安排二面';
                    break;
                case 7:
                    $list[$k]->result_name = '二面fail';
                    break;
                case 8:
                    $list[$k]->result_name = '面试通过';
                    break;
                case 9:
                    $list[$k]->result_name = 'offer中';
                    break;
                case 10:
                    $list[$k]->result_name = '接offer';
                    break;
                case 11:
                    $list[$k]->result_name = '拒offer';
                    break;
                case 12:
                    $list[$k]->result_name = '待入职';
                    break;
                case 13:
                    $list[$k]->result_name = '已入职';
                    break;
                default:
                    $list[$k]->result_name = '待推荐';
            }
        }

        return $list;
    }

    public function getCount($opt=array()){
        $query = self::orderBy('id', 'desc');

        if (!empty($opt)) {
            $query->orWhere(function ($query) use ($opt) {
                foreach ($opt as $k => $v) {
                    if (is_array($v) && $v[1]) {
                        $query = $query->where($k, $v[0], $v[1]);
                    } elseif (!is_array($v) && $v !== false) {
                        $query = $query->where($k, $v);
                    }
                }
            });
        }

        $count = $query->count();

        return $count;
    }


}
